package kingone.phone_store_spring_practice.service.impl;

import kingone.phone_store_spring_practice.dto.OrderDTO;
import kingone.phone_store_spring_practice.entity.OrderMaster;
import kingone.phone_store_spring_practice.entity.PhoneInfo;
import kingone.phone_store_spring_practice.entity.PhoneSpecs;
import kingone.phone_store_spring_practice.enums.PayStatusEnum;
import kingone.phone_store_spring_practice.enums.ResultEnum;
import kingone.phone_store_spring_practice.exception.PhoneException;
import kingone.phone_store_spring_practice.repository.OrderMasterRepository;
import kingone.phone_store_spring_practice.repository.PhoneInfoRepository;
import kingone.phone_store_spring_practice.repository.PhoneSpecsRepository;
import kingone.phone_store_spring_practice.service.OrderService;
import kingone.phone_store_spring_practice.service.PhoneService;
import kingone.phone_store_spring_practice.util.KeyUtil;
import kingone.phone_store_spring_practice.vo.OrderDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private PhoneSpecsRepository phoneSpecsRepository;
    @Autowired
    private PhoneInfoRepository phoneInfoRepository;
    @Autowired
    private OrderMasterRepository orderMasterRepository;
    @Autowired
    private PhoneService phoneService;

    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        //1.此处的orderDTO涉及到多张表OrderMaster、PhoneSpecs、PhoneInfo
        //且需要将orderDto中的内容转移到OrderMaster中
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDTO,orderMaster);  //1.1先将orderDto中和orderMaster相同的部分转移过去

        //1.2将PhoneSpecs中的内容转移过去
        PhoneSpecs phoneSpecs = phoneSpecsRepository.findById(orderDTO.getSpecsId()).get();

        if(phoneSpecs == null){
            log.error("【创建订单】规格不存在，phoneSpecs={}",phoneSpecs);
            throw  new PhoneException(ResultEnum.SPECS_NOT_EXIT);
        }

        BeanUtils.copyProperties(phoneSpecs,orderMaster);

        //1.3将PhoneInfo中的内容转移过去
        PhoneInfo phoneInfo = phoneInfoRepository.findById(phoneSpecs.getPhoneId()).get();

        if(phoneInfo == null){
            log.error("【创建订单】手机不存在，phoneInfo={}",phoneInfo);
            throw  new PhoneException(ResultEnum.PHONE_NOT_EXIT);
        }

        BeanUtils.copyProperties(phoneInfo,orderMaster);

        //2.计算总价：根据订单中的规格ID确定对应产品的价格(因为价格单位为分，所以需要转为元) x 购买数量
        BigDecimal orderAmount = new BigDecimal(0);
        orderAmount = phoneSpecs.getSpecsPrice().divide(new BigDecimal(100))
                .multiply(new BigDecimal(orderDTO.getPhoneQuantity()))
                .add(orderAmount)
                .add(new BigDecimal(10));  //需要加上之前的订单信息
        orderMaster.setOrderAmount(orderAmount);

        //3.设置OrderId信息：当有订单信息时，需要自动生成一个订单号。
        /*且此处不能由数据库自动生成，因此可以借助工具类完成。*/
        orderMaster.setOrderId(KeyUtil.createUniqueKey());
        orderDTO.setOrderId(orderMaster.getOrderId());

        //4.设置支付状态payStatus
        orderMaster.setPayStatus(PayStatusEnum.UNPAID.getCode());

        orderMasterRepository.save(orderMaster);

        //5.修改库存信息
        phoneService.subStock(orderDTO.getSpecsId(),orderDTO.getPhoneQuantity());
        return orderDTO;
    }

    @Override
    public OrderDetailVO findOrderDetailByOrderId(String orderId) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();

        OrderMaster orderMaster = orderMasterRepository.findById(orderId).get();

        if(orderMaster == null){
            log.error("【查询订单】订单不存在，orderMaster={}",orderMaster);
            throw  new PhoneException(ResultEnum.ORDER_NOT_EXIT);
        }

        BeanUtils.copyProperties(orderMaster,orderDetailVO);

        //将订单表里面的BigDecima类型价格转为orderDetailVO中的String类型价格
        orderDetailVO.setSpecsPrice(orderMaster.getSpecsPrice().divide(new BigDecimal(100))+".00");

        return orderDetailVO;
    }

    @Override
    public String pay(String orderId) {
        OrderMaster orderMaster = orderMasterRepository.findById(orderId).get();

        //如果订单不存在，则提示订单为空
        if(orderMaster == null){
            log.error("【支付订单】订单不存在，orderMaster={}",orderMaster);
            throw  new PhoneException(ResultEnum.ORDER_NOT_EXIT);
        }

        //修改支付状态：若果为未支付，则修改为已支付状态；若果
        if(orderMaster.getPayStatus().equals(PayStatusEnum.UNPAID.getCode())){
            orderMaster.setPayStatus(PayStatusEnum.PAID.getCode());
            orderMasterRepository.save(orderMaster);
        } else {
            log.error("【支付订单】订单已支付，orderMaster={}",orderMaster);
        }

        return orderId;
    }
}
